package com.nantai.jiafang.nantaijiafang.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.nantai.jiafang.nantaijiafang.IConstant;
import com.nantai.jiafang.nantaijiafang.MyApplication;
import com.nantai.jiafang.nantaijiafang.R;
import com.nantai.jiafang.nantaijiafang.activity.adapter.UnReadListAdapter;
import com.nantai.jiafang.nantaijiafang.bean.FabricBatchInfo;
import com.nantai.jiafang.nantaijiafang.bean.ProductionOrderInfo;
import com.nantai.jiafang.nantaijiafang.bean.UnReadInfo;

import org.xutils.DbManager;
import org.xutils.common.util.KeyValue;
import org.xutils.db.sqlite.WhereBuilder;
import org.xutils.db.table.DbModel;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class UnReadPushListActivity extends AppCompatActivity {

    private TextView tv_title;
    private TextView tv_back;
    private String flag = "1";//1:生产订单，2：面料批次
    private UnReadListAdapter urla;
    private ListView lv;
    private DbManager db;
    private List<UnReadInfo> list_unread;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_un_read_push_list);
        db = x.getDb(MyApplication.daoConfig);
        initViews();
        setup();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getDataFromDb();
    }

    private void initViews() {
        flag = getIntent().getStringExtra(IConstant.INTENT);
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_back = (TextView) findViewById(R.id.tv_back);
        lv = (ListView) findViewById(R.id.lv_unread);
        if (flag.equals("1")) {
            tv_title.setText("生产订单");
        } else {
            tv_title.setText("面料批次");
        }

    }

    private void getDataFromDb() {
        try {
            List<DbModel> dbModels;
            if (flag.equals("1")) {
                dbModels = db.selector(ProductionOrderInfo.class).where("userId","=", LoginActivity.userId).groupBy("Timestamp").select("Timestamp", "count(Timestamp)", "isRead").orderBy("Timestamp",true).findAll();
            } else {
                dbModels = db.selector(FabricBatchInfo.class).where("userId","=", LoginActivity.userId).groupBy("Timestamp").select("Timestamp", "count(Timestamp)", "isRead").orderBy("Timestamp",true).findAll();
            }
            list_unread = new ArrayList<>();
            if (dbModels != null) {
                for (int i = 0; i < dbModels.size(); i++) {
                    UnReadInfo info = new UnReadInfo();
                    info.setCount(dbModels.get(i).getString("count(Timestamp)"));
                    info.setDate(dbModels.get(i).getString("Timestamp"));
                    info.setIsRead(dbModels.get(i).getString("isRead"));
                    System.out.println(dbModels.get(i).getString("count(Timestamp)") + "===" + dbModels.get(i).getString("Timestamp") + "===" + dbModels.get(i).getString("isRead") + "===" + dbModels.get(i).getString("count(isRead)"));
                    list_unread.add(info);
                }
                urla = new UnReadListAdapter(UnReadPushListActivity.this, list_unread, flag);
                lv.setAdapter(urla);

            }

        } catch (DbException e) {

        }
    }

    private void setup() {
        tv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.putExtra(IConstant.INTENT, list_unread.get(position).getDate() + "");
                if (flag.equals("1")) {
                    intent.setClass(UnReadPushListActivity.this, ProductionOrderUsageActivity.class);
                    try {
                        db.update(ProductionOrderInfo.class,
                                WhereBuilder.b("isRead", "=", "0").b("Timestamp", "=", list_unread.get(position).getDate()).and("userId","=", LoginActivity.userId),
                                new KeyValue("isRead", "1"));
                    } catch (DbException e) {

                    }
                } else {
                    try {
                        db.update(FabricBatchInfo.class,
                                WhereBuilder.b("isRead", "=", "0").b("Timestamp", "=", list_unread.get(position).getDate()).and("userId","=", LoginActivity.userId),
                                new KeyValue("isRead", "1"));
                    } catch (DbException e) {

                    }
                    intent.setClass(UnReadPushListActivity.this, FabricBatchUsageActivity.class);
                }
                startActivity(intent);
            }
        });
    }
}
